package com.it.data_stack.leetCode;

import java.util.LinkedList;

/**
 * @author: Coke
 * @DateTime: 2023/06/17/11:38
 * 逆波兰表达式求值
 **/
public class E02LeetCode120 {
    
    public static int methods (String[] tokens) {
        LinkedList<Integer> stack = new LinkedList<>();
        for (String t : tokens) {
            Integer b, a;
            switch (t) {
                case "+":
                    b = stack.pop();
                    a = stack.pop();
                    stack.push(a + b);
                    break;
                case "-":
                    b = stack.pop();
                    a = stack.pop();
                    stack.push(a - b);
                    break;
                case "*":
                    b = stack.pop();
                    a = stack.pop();
                    stack.push(a * b);
                    break;
                case "/":
                    b = stack.pop();
                    a = stack.pop();
                    stack.push(a / b);
                    break;
                default:
                    stack.push(Integer.parseInt(t));
            }
        }
        return stack.pop();
    }
}
